using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.GamerServices;

namespace Bejeweled.Support
{
    public enum DIAMOND_STATE  {DS_IDLE, DS_ROTATING, DS_MOVING};
    public class Diamond
    {
        public int id;
        public Animation a;
        float x, y;
        float xBoard, yBoard;
        float vx, vy;
        float ax, ay;
        float angle;
        public DIAMOND_STATE state;

        public Diamond()
        {

        }

        public Diamond(int id, float xBoard, float yBoard)
        {
            this.id = id;
            this.xBoard = xBoard;
            this.yBoard = yBoard;
            this.x = xBoard * 60;
            this.y = yBoard * 60;

            a = new Animation(RscMgr.GetInstance().tDiamond, 7, 20, 0.001f);
            SetState(DIAMOND_STATE.DS_IDLE);
        }


        public void Update(float t)
        {
            switch(state)
            {
                case DIAMOND_STATE.DS_MOVING:
                    UpdatePosition(t);
                    break;
                case DIAMOND_STATE.DS_ROTATING:
                    a.Update(id * a.numColumn, (id + 1) * a.numColumn - 1, t);
                    break;
                case DIAMOND_STATE.DS_IDLE:
                    a.SetIndex(id * a.numColumn);
                    break;
            }
            
        }

        public void UpdatePosition(float t)
        {
            x += vx * t + 0.5f * ax * t * t;
            y += vy * t + 0.5f * ay * t * t;
        }

        public void SetState(DIAMOND_STATE state)
        {
            this.state = state;
            switch (state)
            {
                case DIAMOND_STATE.DS_IDLE:
                    vx = vy = 0;
                    ax = ay = 0;
                    a.SetIndex(id * a.numColumn);
                    break;

            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //Rectangle dRect = new Rectangle((int)(100 + x * (60)), (int)y * (60), 60, 60);
            //Rectangle sRect = a.rect;
            //spriteBatch.Draw(RscMgr.GetInstance().tDiamond, dRect, sRect, Color.White);
            spriteBatch.Draw(RscMgr.GetInstance().tDiamond, new Rectangle((int)(x), (int)(200 + y), 64, 64), a.rect, Color.White);
        }
    }
}
